﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;


namespace BadHabit.Factory {

    /// <summary>
    /// This is another part of the client.
    /// It uses the old friend NotifyIcon from Windows Form time and emulates real double-clicks
    /// at the icon.
    /// </summary>
    public partial class Client : Application {

        // the icon
        private static System.Windows.Forms.NotifyIcon notifyIcon;
        private static System.Drawing.Icon oldIcon = BadHabit.Factory.Properties.Resources.factoryDisconnected; // used at the icon's change

        // used to differ a single from a double click
        private static int clickDelay = 0;
        private static bool isDoubleClick = false;
        private static System.Windows.Forms.Timer clickTimer = new System.Windows.Forms.Timer();

        
    

        /// <summary>
        /// Creates the notify icon using the old windows form framework.
        /// We still waiting bill to release this component at WPF.
        /// </summary>
        private static void CreateNotifyIcon() {
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.Icon = Factory.Properties.Resources.factoryDisconnected;
            notifyIcon.Visible = true;

            
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseDoubleClick);

            // have to use the Forms version
            System.Windows.Forms.ContextMenuStrip contextMenu = new System.Windows.Forms.ContextMenuStrip { Name = "contextMenu" };
            contextMenu.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            contextMenu.ShowImageMargin = false;
            System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "exitToolStripMenuItem", Text = BadHabit.Factory.Properties.Resources.Exit };
            System.Windows.Forms.ToolStripMenuItem mainWindowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "mainWindowToolStripMenuItem", Text = BadHabit.Factory.Properties.Resources.Go_to_project_cockpit };
            System.Windows.Forms.ToolStripMenuItem moreTasksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "moreTasksToolStripMenuItem", Text = BadHabit.Factory.Properties.Resources.Give_me_more_tasks };
            System.Windows.Forms.ToolStripMenuItem connectTasksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "ConnectToolStripMenuItem", Text = BadHabit.Factory.Properties.Resources.Re_connect };
            System.Windows.Forms.ToolStripMenuItem settingsTasksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "SettingsToolStripMenuItem", Text = BadHabit.Factory.Properties.Resources.Settings };
            System.Windows.Forms.ToolStripMenuItem teamAllocationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "TeamAllocationToolStripMenuItem", Text = BadHabit.Factory.Properties.Resources.Team_aAllocation };

            mainWindowToolStripMenuItem.Click += new EventHandler(mainWindowToolStripMenuItem_Click);
            exitToolStripMenuItem.Click += new EventHandler(exitToolStripMenuItem_Click);
            moreTasksToolStripMenuItem.Click += new EventHandler(moreTasksToolStripMenuItem_Click);
            settingsTasksToolStripMenuItem.Click += new EventHandler(settingsTasksToolStripMenuItem_Click);
            connectTasksToolStripMenuItem.Click += new EventHandler(connectTasksToolStripMenuItem_Click);
            teamAllocationToolStripMenuItem.Click += new EventHandler(teamAllocationToolStripMenuItem_Click);

            // contextMenu
            contextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { mainWindowToolStripMenuItem, moreTasksToolStripMenuItem, settingsTasksToolStripMenuItem, teamAllocationToolStripMenuItem, connectTasksToolStripMenuItem, new System.Windows.Forms.ToolStripSeparator(), exitToolStripMenuItem });
            notifyIcon.ContextMenuStrip = contextMenu;

            // sets the doubleClick timer
            clickTimer.Interval = 50;
            clickTimer.Tick += new EventHandler(clickTimer_Tick);

            

        }

        static void teamAllocationToolStripMenuItem_Click(object sender, EventArgs e) {
            Windows.TeamAllocation w = new Windows.TeamAllocation();
            w.Show();
        }

    

        /// <summary>
        /// Tries to reconnect to the service.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void connectTasksToolStripMenuItem_Click(object sender, EventArgs e) {
            Connect(true);
        }

        

        /// <summary>
        /// Shows settings form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void settingsTasksToolStripMenuItem_Click(object sender, EventArgs e) {
            BadHabit.Factory.Windows.Main.ShowSettingsWindow(false);
        }

        

        /// <summary>
        /// Bind again the tasks to see if there is anything new.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void moreTasksToolStripMenuItem_Click(object sender, EventArgs e) {
            TaskSelector.Bind();
        }

        /// <summary>
        /// When the notify icon exit menu item is clicked, shutdown the application.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            EndApplication();
        }

        /// <summary>
        /// When the notify icon main window menu item is clicked, show the main window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void mainWindowToolStripMenuItem_Click(object sender, EventArgs e) {
            ShowMainWindow();
        }

        /// <summary>
        /// Starts a timer to differ a single from a double click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                return;

            // if not using task track, just open the window
            if (!BadHabit.Factory.Properties.Settings.Default.TaskTrackActive) {
                ShowMainWindow();
                return;
            }

            isDoubleClick = false;
            clickTimer.Start();
        }

        /// <summary>
        /// Just set a double click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void notifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e) {
            isDoubleClick = true;
        }

        /// <summary>
        /// Occurs every 50 miliseconds after a click.
        /// When the Double Click time has already past identify what action should be taken:
        /// SlideUp (single click) or ShowMainWindow (double click).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void clickTimer_Tick(object sender, EventArgs e) {
            clickDelay += clickTimer.Interval;
            if (clickDelay >= System.Windows.Forms.SystemInformation.DoubleClickTime) {
                clickDelay = 0;
                clickTimer.Stop();
                if (isDoubleClick)
                    ShowMainWindow();
                else
                    Client.TaskSelector.SlideUp();
            }
        }

               
        /// <summary>
        /// Sets the reading icon when reading.
        /// </summary>
        public static bool IsReadingData {
            set {
                if (value) {
                    if (FactoryMainWindow == null)
                        return;
                    FactoryMainWindow.IsWaiting = true;
                    oldIcon = notifyIcon.Icon;
                    notifyIcon.Icon = BadHabit.Factory.Properties.Resources.factoryReading;                                        
                }
                else {
                    if (FactoryMainWindow == null)
                        return;
                    FactoryMainWindow.IsWaiting = false;
                    notifyIcon.Icon = oldIcon;                                        
                }
            }
        }



        /// <summary>
        /// Show the alert with the number of new tasks for the user.
        /// </summary>
        /// <param name="count">The number of tasks</param>
        public static void ShowTaskAlertToolTip(int count) {
            StringBuilder msg = new StringBuilder();
            msg.AppendFormat(BadHabit.Factory.Properties.Resources.You_have_N_new_task, count);
            notifyIcon.ShowBalloonTip(2000, "Factory", msg.ToString(), System.Windows.Forms.ToolTipIcon.Info);
        }


        public static string WorkingOnTaskName {
            set {
                //notifyIcon.Text = "Working on '" + value + "'";
            }
        }

    }

}
